package com.nf.controller.login;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.UserInfoDao;
import com.nf.dao.impl.UserInfoDaoImpl;
import com.nf.entity.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 编写servle，实现用户注册时验证其账号名是否存在
 */
@WebServlet("/api/UserCheckServlet.do")
public class UserCheckServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name=req.getParameter("userName");

        UserInfoDao dao=new UserInfoDaoImpl();
        UserInfo user=dao.checkUser(name);

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        out.print(new Gson().toJson(successJson(user)));
        out.flush();
        out.close();
    }
}
